<template>
  <table>
    <thead>
      <tr>
        <th
          v-for="(element, index) in headerList"
          :key="index"
          :style="{ width: element.config.width }"
        >
          {{ element.name }}
        </th>
      </tr>
    </thead>
    <tbody>
      <template v-for="(row, index) in formDate" :key="index">
        <tr class="content_box">
          <td colspan="4">
            <div class="title_box">
              <div
                class="title_icon"
                :style="{ backgroundColor: row.color ? row.color : '' }"
              ></div>
              <div class="title_item">{{ row.title }}</div>
            </div>
          </td>
        </tr>

        <tr class="content_box" v-for="(item, itemIndex) in row.list" :key="itemIndex">
          <td style="text-align: left">{{ item.name }}</td>
          <td>{{ item.price }}</td>
          <td style="color: red"><span v-if="item.discount">-</span>{{ item.discount }}</td>
          <td>{{ item.expend }}</td>
        </tr>
      </template>
      <tr class="content_box bottom_box">
        <td style="text-align: start; font-weight: 700">合计:</td>
        <td>{{ data.price }}</td>
        <td style="color: red">-{{ data.discount }}</td>
        <td>{{ data.expend }}</td>
      </tr>
      <tr class="content_box bottom_box" v-if="data.back_fee && Number(data.back_fee) > 0">
        <td colspan="2" style="text-align: end">活动返费:</td>
        <td></td>
        <td>-{{ data.back_fee }}</td>
      </tr>
      <tr class="content_box bottom_box" v-if="data.deal_fee && Number(data.deal_fee) > 0">
        <td colspan="2" style="text-align: end">使用专款:</td>
        <td></td>
        <td>-{{ data.deal_fee }}</td>
      </tr>
      <tr class="content_box bottom_box">
        <td colspan="2" style="text-align: end; font-size: 15px; font-weight: 700">实际应付:</td>
        <td></td>
        <td style="color: blue; font-size: 15px; font-weight: 700">{{ data.final_pay_fee }}</td>
      </tr>
    </tbody>
  </table>
</template>

<script setup lang="ts">
import { ref, defineProps, defineEmits, onMounted } from 'vue'
const headerList = [
  {
    name: '消费项目',
    config: {
      width: 'auto'
    }
  },
  {
    name: '价格',
    config: {
      width: '67px'
    }
  },
  {
    name: '减免',
    config: {
      width: '67px'
    }
  },
  {
    name: '实际消费',
    config: {
      width: '95px'
    }
  }
]
const colorList = ref(['coral', '#91f56d', '#1e96ff', '#5b41fe', '#fd58f8', '#25f287'])
const props = defineProps({
  data: {
    type: Object,
    required: true
  }
})
const formDate = ref([])
onMounted(() => {
  getData()
})
function getData() {
  formDate.value = props.data.tableData.map((obj: { color: any }, index: number) => {
    obj.color = colorList.value[index % colorList.value.length]
    return obj
  })
}
</script>

<style scoped lang="less">
table {
  width: 100%;
  border: 1px solid #eaeaea;
  border-collapse: collapse; /*合并边框 */
  tbody {
    width: 100%;

    .content_box {
      width: 100%;

      td {
        padding: 5px 7px;
        font-size: 13px;
      }
      .title_box {
        width: 100%;
        display: flex;
        align-items: center;
        padding: 2px 4px;
        box-sizing: border-box;
        .title_icon {
          width: 9px;
          height: 9px;
          background-color: aqua;
          border-radius: 50%;
        }
        .title_item {
          margin-left: 8px;
          font-size: 13px;
          font-weight: bold;
        }
      }
    }
    .bottom_box {
      border: #f2f2f2 1px solid;
      background-color: aliceblue;
    }
  }
}

th {
  color: #333;
  font-size: 14px;
  font-weight: 500;
  text-align: center;
  background-color: #f2f2f2;
  padding: 8px 0px;
}
td {
  text-align: center;
}
</style>
